package com.hllxd.hism.star.basic.service.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hllxd.hism.star.basic.service.dao.mapper.mis.ProductMapper;
import com.hllxd.hism.star.basic.service.model.order.ProductEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * author: xiyong.lxy
 * date: 2020-10-12 16:51
 */
@Slf4j
@Service("productService")
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override public List<ProductEntity> hotProduct() {
        log.info("list product service.");

        List<ProductEntity> items = productMapper.hotProduct();
        log.info("items: {}",items.size());
        return items;
    }

    @Override public List<ProductEntity> listProduct() {
        return productMapper.listProduct();
    }

    @Override public ProductEntity getProduct(Long ID) {
        log.info("getProduct {}", ID);
        return productMapper.getProduct(ID);
    }

    @Override public JSONObject listProductByAttr() {
        log.info("list product service.");

        List<ProductEntity> yjItems = productMapper.listProductByAttr("YJ");
        List<ProductEntity> rjItems = productMapper.listProductByAttr("RJ");
        List<ProductEntity> llItems = productMapper.listProductByAttr("LL");
        JSONObject obj = new JSONObject();
        obj.put("shopProducts",yjItems);
        obj.put("scmProducts", rjItems);
        obj.put("bigdataProducts",llItems);
        log.info("items: {},{},{}.",yjItems.size(), rjItems.size(), llItems.size());
        return obj;
    }


    @Override public Long setProduct(ProductEntity entity) {
        log.info("set param: {}", JSON.toJSONString(entity));
        if(entity.ID==null || entity.ID==0) {
            return productMapper.insertProduct(entity);
        } else {
            return productMapper.updateProduct(entity);
        }
    }

}
